<template>
  <div class="app-container">
    <el-row type="flex" justify="space-between" class="mb10">
      <el-col>
        <el-button size="small" @click="handleBack">返回上一页</el-button>
        <span style="font-weight: bold; margin-left: 10px">采购合同-调价</span>
      </el-col>
      <div>
        <!-- <el-button type="primary" size="mini" @click="handleSubmit"
          >提交</el-button
        > -->
      </div>
    </el-row>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="调价状态" prop="oneCategoryId">
        <el-select
          v-model="queryParams.oneCategoryId"
          @change="changeOne"
          placeholder="请选择"
        >
          <el-option label="请选择" :value="null"></el-option>
          <el-option
            v-for="(item, index) in categoryList1"
            :key="index"
            :label="item.categoryName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="40" label="原材料名称" prop="id">
        <el-input
          type="number"
          v-model="queryParams.id"
          placeholder="ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:designator:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="designatorList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="ID" width="55" align="center" prop="id" />
      <el-table-column label="合同编号" align="center" prop="name" />
      <el-table-column
        label="合同创建时间"
        align="center"
        prop="intensityRating"
      />
      <el-table-column
        label="供应商名称"
        align="center"
        prop="categoryInfoId"
      />
      <el-table-column
        label="原材料名称"
        align="center"
        prop="plasticHoldingTime"
      />
      <el-table-column
        label="是否有抵顶物"
        align="center"
        prop="intensityRating"
      />
      <el-table-column
        label="累计结算金额"
        align="center"
        prop="benchmarkPrice"
      />
      <el-table-column
        label="累计应付金额"
        align="center"
        prop="threeCategoryName"
      />
      <el-table-column label="合同状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.status == '0'"
            @change="changeStatus(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        width="190px"
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleChangePrice(scope.row)"
            v-hasPermi="['system:designator:remove']"
            >调价协议</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:designator:edit']"
            >取消调价</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
  
  <script>
import {
  listDesignator,
  getDesignator,
  delDesignator,
  addDesignator,
  updateDesignator,
} from "@/api/service/main";
import NumberFormItem from "@/components/NumberFormItem";
import { listOnInfo } from "@/api/service/info";
export default {
  components: {
    NumberFormItem,
  },
  name: "Designator",
  data() {
    return {
      // 重置密码是否展示
      resetPassword: {
        visible: false,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品--主标号表格数据
      designatorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        oneCategoryId: null,
        twoCategoryId: null,
        threeCategoryId: null,
      },
      // 表单参数
      form: {},
      form1: {}, // 预拌混凝土
      form2: {}, // 预拌砂浆

      // 表单校验
      rules: {
        oneCategoryId: [{ required: true, message: "请选择", trigger: "blur" }],
        twoCategoryId: [{ required: true, message: "请选择", trigger: "blur" }],
        threeCategoryId: [
          { required: true, message: "请选择", trigger: "blur" },
        ],
        type: [{ required: true, message: "请选择", trigger: "blur" }],
        name: [
          {
            required: true,
            message: "主标号名称不能为空",
            trigger: "blur",
          },
        ],
        status: [{ required: true, message: "请选择", trigger: "blur" }],
        intensityRating: [
          {
            required: true,
            message: "强度等级不能为空",
            trigger: "blur",
          },
        ],
      },

      activeName: "1",
      categoryList1: [],
      categoryList2: [],
      categoryList3: [],

      formCategoryList1: [],
      formCategoryList2: [],
      formCategoryList3: [],
    };
  },
  created() {
    this.getList();
    this.getListOnInfo();
  },
  computed: {
    title() {
      return !this.$route?.query?.id ? "新增采购合同" : "修改采购合同";
    },
  },
  methods: {
    handleBack() {
      this.$router.push({
        path: "/purchase/contract/index",
      });
    },
    // 开关
    changeStatus(item) {
      const form = { ...item };

      // 改变状态
      form.status = form.status == "0" ? 1 : 0;

      updateDesignator(form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        item.status = form.status;
        //this.getList();
      });
    },
    formChangeOne() {
      this.form.twoCategoryId = null;
      this.form.threeCategoryId = null;
      this.formCategoryList2 = [];
      this.formCategoryList3 = [];
      if (!this.form.oneCategoryId) {
        return;
      }
      listOnInfo({
        type: 2,
        oneCategoryId: this.form.oneCategoryId,
      }).then((response) => {
        this.formCategoryList2 = response.data;
      });
    },
    formChangeTwo() {
      this.form.threeCategoryId = null;
      this.formCategoryList3 = [];
      if (!this.form.twoCategoryId) {
        return;
      }
      listOnInfo({
        type: 3,
        oneCategoryId: this.form.oneCategoryId,
        twoCategoryId: this.form.twoCategoryId,
      }).then((response) => {
        this.formCategoryList3 = response.data;
      });
    },
    formChangeThree() {
      this.form.categoryInfoId = this.formCategoryList3.find(
        (item) => item.id == this.form.threeCategoryId
      )?.categoryInfoId;
      //    this.form.categoryInfoId = this.form.threeCategoryId;
    },
    changeOne() {
      this.queryParams.twoCategoryId = null;
      this.queryParams.threeCategoryId = null;
      this.categoryList2 = [];
      this.categoryList3 = [];
      if (!this.queryParams.oneCategoryId) {
        return;
      }
      listOnInfo({
        type: 2,
        oneCategoryId: this.queryParams.oneCategoryId,
      }).then((response) => {
        this.categoryList2 = response.data;
      });
    },
    changeTwo() {
      this.queryParams.threeCategoryId = null;
      this.categoryList3 = [];
      if (!this.queryParams.twoCategoryId) {
        return;
      }
      listOnInfo({
        type: 3,
        oneCategoryId: this.queryParams.oneCategoryId,
        twoCategoryId: this.queryParams.twoCategoryId,
      }).then((response) => {
        this.categoryList3 = response.data;
      });
    },
    // 查询 一级 二级 三级 目录数据
    getListOnInfo() {
      listOnInfo({ type: 1 }).then((res) => {
        this.categoryList1 = res.data;
        this.formCategoryList1 = res.data;
      });
    },
    /** 查询产品--主标号列表 */
    getList() {
      this.loading = true;
      listDesignator(this.queryParams).then((response) => {
        this.designatorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.activeName = "1";
      this.form = {
        id: null,
        type: null,
        oneCategoryId: null,
        twoCategoryId: null,
        threeCategoryId: null,
        categoryInfoId: null,
        name: null,
        plasticHoldingTime: null,
        intensityRating: null,
        benchmarkPrice: null,
        content: null,
        status: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };

      this.form1 = {
        pldh: null,
        pmdh: null,
        bgmd: null,
        dldykzddj: null,
        kzddjykzzj: null,
        kdf: null,
        mdf: null,
        ksdj: null,
        klsydj: null,
        kllzstdj: null,
        kllzstdjdtlf: null,
        kthxndj: null,
        zmsxndjyxn: null,
        tsxs: null,
        kzdj: null,
        gmddj: null,
        xsldj: null,
        fhdj: null,
      };
      this.form2 = {
        pldh: null,
        pmdh: null,
        bgmd: null,
        cd: null,
        bsl: null,
        kdxqdssl: null,
        kdxzlssl: null,
        njlsqd: null,
        ssl: null,
        ksdj: null,
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      //   this.open = true;
      //   this.title = "新增主标号";
      this.$router.push({
        path: "/purchase/contract/edit",
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: `/purchase/contract/edit?id=${row.userId}`,
      });
      return;
      this.reset();
      const id = row.id || this.ids;
      getDesignator(id).then((response) => {
        this.form = response.data;

        if (this.form.type == 1) {
          this.form1 = JSON.parse(this.form.content || "{}");
        }
        if (this.form.type == 2) {
          this.form2 = JSON.parse(this.form.content || "{}");
        }

        listOnInfo({
          type: 2,
          oneCategoryId: this.form.oneCategoryId,
        }).then((response) => {
          this.formCategoryList2 = response.data;
        });
        listOnInfo({
          type: 3,
          oneCategoryId: this.form.oneCategoryId,
          twoCategoryId: this.form.twoCategoryId,
        }).then((response) => {
          this.formCategoryList3 = response.data;
        });

        this.open = true;
        this.title = "修改主标号";
      });
    },
    /** 提交按钮 */
    async submitForm() {
      await Promise.all([
        this.$refs["form-left"].validate(),
        this.$refs["form-type"].validate(),
        this.$refs["form-right"].validate(),
      ]);

      if (this.form.type == 1) {
        this.form.content = JSON.stringify(this.form1);
      }
      if (this.form.type == 2) {
        this.form.content = JSON.stringify(this.form2);
      }

      if (this.form.id != null) {
        updateDesignator(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addDesignator(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }

      //   this.$refs["form"].validate(valid => {
      //     if (valid) {
      //       if (this.form.id != null) {
      //         updateDesignator(this.form).then(response => {
      //           this.$modal.msgSuccess("修改成功");
      //           this.open = false;
      //           this.getList();
      //         });
      //       } else {
      //         addDesignator(this.form).then(response => {
      //           this.$modal.msgSuccess("新增成功");
      //           this.open = false;
      //           this.getList();
      //         });
      //       }
      //     }
      //   });
    },
    /** 调价 */
    handleChangePrice(row) {
      this.$router.push({
        path: `/purchase/contract/change-price?id=${row.userId}`,
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/designator/export",
        {
          ...this.queryParams,
        },
        `designator_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.edit-form {
  // display: flex;
  .form-label {
    font-size: 14px;
    font-weight: bold;
    line-height: 36px;
    padding-right: 30px;
    .form-red {
      color: #d23c26;
    }
  }
  .form-value {
    display: flex;
    //gap: 30px;
  }
}

::v-deep .el-tabs__content {
  max-height: 500px;
  overflow: auto;
}

.row-item {
  display: flex;
  // gap: 20px;
}
::v-deep .el-form-item__label {
  // width: 240px;
  text-align: right;
}
.form-right ::v-deep .el-input {
  width: 200px;
}
</style>